home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / trial / demon / TURNPIKE.1 / CLASSES.ZIP / JAVA / LANG / ThreadGroup.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-14  |  4.6 KB  |  376 lines

  1. package java.lang;
  2.  
  3. import java.io.PrintStream;
  4. import ms.applet.AppletViewer;
  5.  
  6. public class ThreadGroup {
  7.    ThreadGroup parent;
  8.    String name;
  9.    int maxPriority;
  10.    boolean destroyed;
  11.    boolean daemon;
  12.    int nthreads;
  13.    Thread[] threads;
  14.    int ngroups;
  15.    ThreadGroup[] groups;
  16.  
  17.    private ThreadGroup() {
  18.       this.name = "system";
  19.       this.maxPriority = 10;
  20.    }
  21.  
  22.    public ThreadGroup(String var1) {
  23.       Thread var2 = Thread.currentThread();
  24.       this(var2.group, var1);
  25.    }
  26.  
  27.    public ThreadGroup(ThreadGroup var1, String var2) {
  28.       if (var1 == null) {
  29.          throw new NullPointerException();
  30.       } else {
  31.          var1.checkAccess();
  32.          this.name = var2;
  33.          this.maxPriority = var1.maxPriority;
  34.          this.daemon = var1.daemon;
  35.          this.parent = var1;
  36.          var1.add(this);
  37.       }
  38.    }
  39.  
  40.    public final String getName() {
  41.       return this.name;
  42.    }
  43.  
  44.    public final ThreadGroup getParent() {
  45.       return this.parent;
  46.    }
  47.  
  48.    public final int getMaxPriority() {
  49.       return this.maxPriority;
  50.    }
  51.  
  52.    public final boolean isDaemon() {
  53.       return this.daemon;
  54.    }
  55.  
  56.    public final void setDaemon(boolean var1) {
  57.       this.checkAccess();
  58.       this.daemon = var1;
  59.    }
  60.  
  61.    public final synchronized void setMaxPriority(int var1) {
  62.       this.checkAccess();
  63.       if (var1 < 1) {
  64.          this.maxPriority = 1;
  65.       } else if (var1 < this.maxPriority) {
  66.          this.maxPriority = var1;
  67.       }
  68.  
  69.       for(int var2 = 0; var2 < this.ngroups; ++var2) {
  70.          this.groups[var2].setMaxPriority(var1);
  71.       }
  72.  
  73.    }
  74.  
  75.    public final boolean parentOf(ThreadGroup var1) {
  76.       while(var1 != null) {
  77.          if (var1 == this) {
  78.             return true;
  79.          }
  80.  
  81.          var1 = var1.parent;
  82.       }
  83.  
  84.       return false;
  85.    }
  86.  
  87.    public final void checkAccess() {
  88.       SecurityManager var1 = System.security;
  89.       if (var1 != null) {
  90.          var1.checkAccess(this);
  91.       }
  92.  
  93.    }
  94.  
  95.    public synchronized int activeCount() {
  96.       if (this.destroyed) {
  97.          return 0;
  98.       } else {
  99.          int var1 = this.nthreads;
  100.  
  101.          for(int var2 = 0; var2 < this.ngroups; ++var2) {
  102.             var1 += this.groups[var2].activeCount();
  103.          }
  104.  
  105.          return var1;
  106.       }
  107.    }
  108.  
  109.    public int enumerate(Thread[] var1) {
  110.       return this.enumerate((Thread[])var1, 0, true);
  111.    }
  112.  
  113.    public int enumerate(Thread[] var1, boolean var2) {
  114.       return this.enumerate((Thread[])var1, 0, var2);
  115.    }
  116.  
  117.    private synchronized int enumerate(Thread[] var1, int var2, boolean var3) {
  118.       if (this.destroyed) {
  119.          return 0;
  120.       } else {
  121.          int var4 = this.nthreads;
  122.          if (var4 > var1.length - var2) {
  123.             var4 = var1.length - var2;
  124.          }
  125.  
  126.          if (var4 > 0) {
  127.             System.arraycopy(this.threads, 0, var1, var2, var4);
  128.             var2 += var4;
  129.          }
  130.  
  131.          if (var3) {
  132.             for(int var5 = 0; var5 < this.ngroups; ++var5) {
  133.                var2 = this.groups[var5].enumerate(var1, var2, true);
  134.             }
  135.          }
  136.  
  137.          return var2;
  138.       }
  139.    }
  140.  
  141.    public synchronized int activeGroupCount() {
  142.       if (this.destroyed) {
  143.          return 0;
  144.       } else {
  145.          int var1 = this.ngroups;
  146.  
  147.          for(int var2 = 0; var2 < this.ngroups; ++var2) {
  148.             var1 += this.groups[var2].activeGroupCount();
  149.          }
  150.  
  151.          return var1;
  152.       }
  153.    }
  154.  
  155.    public int enumerate(ThreadGroup[] var1) {
  156.       return this.enumerate((ThreadGroup[])var1, 0, true);
  157.    }
  158.  
  159.    public int enumerate(ThreadGroup[] var1, boolean var2) {
  160.       return this.enumerate((ThreadGroup[])var1, 0, var2);
  161.    }
  162.  
  163.    private synchronized int enumerate(ThreadGroup[] var1, int var2, boolean var3) {
  164.       if (this.destroyed) {
  165.          return 0;
  166.       } else {
  167.          int var4 = this.ngroups;
  168.          if (var4 > var1.length - var2) {
  169.             var4 = var1.length - var2;
  170.          }
  171.  
  172.          if (var4 > 0) {
  173.             System.arraycopy(this.groups, 0, var1, var2, var4);
  174.             var2 += var4;
  175.          }
  176.  
  177.          if (var3) {
  178.             for(int var5 = 0; var5 < this.ngroups; ++var5) {
  179.                var2 = this.groups[var5].enumerate(var1, var2, true);
  180.             }
  181.          }
  182.  
  183.          return var2;
  184.       }
  185.    }
  186.  
  187.    public final synchronized void stop() {
  188.       this.checkAccess();
  189.  
  190.       for(int var1 = 0; var1 < this.ngroups; ++var1) {
  191.          this.groups[var1].stop();
  192.       }
  193.  
  194.       for(int var2 = 0; var2 < this.nthreads; ++var2) {
  195.          Thread var3 = this.threads[var2];
  196.          var3.stop(new ThreadDeath());
  197.       }
  198.  
  199.    }
  200.  
  201.    public final synchronized void suspend() {
  202.       this.checkAccess();
  203.  
  204.       for(int var1 = 0; var1 < this.ngroups; ++var1) {
  205.          this.groups[var1].suspend();
  206.       }
  207.  
  208.       for(int var2 = 0; var2 < this.nthreads; ++var2) {
  209.          Thread var3 = this.threads[var2];
  210.          var3.checkAccess();
  211.          var3.suspend0();
  212.       }
  213.  
  214.    }
  215.  
  216.    public final synchronized void resume() {
  217.       this.checkAccess();
  218.  
  219.       for(int var1 = 0; var1 < this.ngroups; ++var1) {
  220.          this.groups[var1].resume();
  221.       }
  222.  
  223.       for(int var2 = 0; var2 < this.nthreads; ++var2) {
  224.          Thread var3 = this.threads[var2];
  225.          var3.checkAccess();
  226.          var3.resume0();
  227.       }
  228.  
  229.    }
  230.  
  231.    public final synchronized void destroy() {
  232.       boolean var1 = false;
  233.       AppletViewer var2 = AppletViewer.getAppletViewer();
  234.       if (var2 != null && var2.closing()) {
  235.          var1 = true;
  236.       }
  237.  
  238.       if (!var1) {
  239.          this.checkAccess();
  240.       }
  241.  
  242.       if (!this.destroyed && (var1 || this.nthreads <= 0)) {
  243.          while(this.ngroups > 0) {
  244.             this.groups[0].destroy();
  245.          }
  246.  
  247.          if (this.parent != null) {
  248.             this.destroyed = true;
  249.             this.groups = null;
  250.             this.threads = null;
  251.             this.parent.remove(this);
  252.          }
  253.  
  254.       } else {
  255.          throw new IllegalThreadStateException();
  256.       }
  257.    }
  258.  
  259.    private final synchronized void add(ThreadGroup var1) {
  260.       if (this.destroyed) {
  261.          throw new IllegalThreadStateException();
  262.       } else {
  263.          if (this.groups == null) {
  264.             this.groups = new ThreadGroup[4];
  265.          } else if (this.ngroups == this.groups.length) {
  266.             ThreadGroup[] var2 = new ThreadGroup[this.ngroups * 2];
  267.             System.arraycopy(this.groups, 0, var2, 0, this.ngroups);
  268.             this.groups = var2;
  269.          }
  270.  
  271.          this.groups[this.ngroups] = var1;
  272.          ++this.ngroups;
  273.       }
  274.    }
  275.  
  276.    private synchronized void remove(ThreadGroup var1) {
  277.       if (!this.destroyed) {
  278.          for(int var2 = 0; var2 < this.ngroups; ++var2) {
  279.             if (this.groups[var2] == var1) {
  280.                System.arraycopy(this.groups, var2 + 1, this.groups, var2, --this.ngroups - var2);
  281.                this.groups[this.ngroups] = null;
  282.                break;
  283.             }
  284.          }
  285.  
  286.          if (this.nthreads == 0) {
  287.             this.notifyAll();
  288.          }
  289.  
  290.          if (this.daemon && this.nthreads == 0 && this.ngroups == 0) {
  291.             this.destroy();
  292.          }
  293.  
  294.       }
  295.    }
  296.  
  297.    synchronized void add(Thread var1) {
  298.       if (this.destroyed) {
  299.          throw new IllegalThreadStateException();
  300.       } else {
  301.          if (this.threads == null) {
  302.             this.threads = new Thread[4];
  303.          } else if (this.nthreads == this.threads.length) {
  304.             Thread[] var2 = new Thread[this.nthreads * 2];
  305.             System.arraycopy(this.threads, 0, var2, 0, this.nthreads);
  306.             this.threads = var2;
  307.          }
  308.  
  309.          this.threads[this.nthreads] = var1;
  310.          ++this.nthreads;
  311.       }
  312.    }
  313.  
  314.    synchronized void remove(Thread var1) {
  315.       if (!this.destroyed) {
  316.          for(int var2 = 0; var2 < this.nthreads; ++var2) {
  317.             if (this.threads[var2] == var1) {
  318.                System.arraycopy(this.threads, var2 + 1, this.threads, var2, --this.nthreads - var2);
  319.                this.threads[this.nthreads] = null;
  320.                break;
  321.             }
  322.          }
  323.  
  324.          if (this.nthreads == 0) {
  325.             this.notifyAll();
  326.          }
  327.  
  328.          if (this.daemon && this.nthreads == 0 && this.ngroups == 0) {
  329.             this.destroy();
  330.          }
  331.  
  332.       }
  333.    }
  334.  
  335.    public synchronized void list() {
  336.       this.list(System.out, 0);
  337.    }
  338.  
  339.    void list(PrintStream var1, int var2) {
  340.       for(int var3 = 0; var3 < var2; ++var3) {
  341.          var1.print(" ");
  342.       }
  343.  
  344.       var1.println(this);
  345.       var2 += 4;
  346.  
  347.       for(int var4 = 0; var4 < this.nthreads; ++var4) {
  348.          for(int var5 = 0; var5 < var2; ++var5) {
  349.             var1.print(" ");
  350.          }
  351.  
  352.          var1.println(this.threads[var4]);
  353.       }
  354.  
  355.       for(int var7 = 0; var7 < this.ngroups; ++var7) {
  356.          this.groups[var7].list(var1, var2);
  357.       }
  358.  
  359.    }
  360.  
  361.    public void uncaughtException(Thread var1, Throwable var2) {
  362.       if (this.parent != null) {
  363.          this.parent.uncaughtException(var1, var2);
  364.       } else {
  365.          if (!(var2 instanceof ThreadDeath)) {
  366.             var2.printStackTrace(System.err);
  367.          }
  368.  
  369.       }
  370.    }
  371.  
  372.    public String toString() {
  373.       return this.getClass().getName() + "[name=" + this.name + ",maxpri=" + this.maxPriority + "]";
  374.    }
  375. }
  376.